﻿
using HBase;
using System;
using System.Collections.Generic;
using System.Data;

namespace hCommand {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：LocSelect                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2021/9/8 16:59:49                        
   *└────────────────────────────────────────────────┘
   **/
    public class LocSelectDouble :LocSelect {

        public override CoreWhLoc select(List<CoreWhLoc> locList ,long goodDiff) {
            CoreWhLoc loc1 = null;
            CoreWhLoc loc2 = null;
            CoreWhLoc loc3 = null;
            CoreWhLoc loc4 = null;
            List<DoubleLoc> doubleLocList = initLoc(locList);
            foreach( DoubleLoc doubleLoc in doubleLocList ) {
                loc1 = selectDoubleSame(loc1 ,doubleLoc ,goodDiff);
                if( loc1 != null ) {
                    return loc1;
                }
                loc2 = selectDeep(loc2 ,doubleLoc);
                if( loc2 == null ) {
                    loc3 = selectSingle(loc3 ,doubleLoc);
                    if( loc3 == null ) {
                        loc4 = selectDoubleDef(loc4 ,doubleLoc ,goodDiff);
                    }
                }
            }
            if( loc2 != null ) {
                return loc2;
            }
            if( loc3 != null ) {
                return loc3;
            }
            return loc4;
        }
        static CoreWhLoc selectDeep(CoreWhLoc select ,DoubleLoc doubleLoc) {
            if( select != null ) {
                return select;
            }
            CoreWhLoc deep = doubleLoc.deep;
            if( deep == null ) {
                return select;
            }
            if( deep.countStock == 0 ) {
                return deep;
            }

            return select;
        }
        static CoreWhLoc selectDoubleSame(CoreWhLoc select ,DoubleLoc doubleLoc ,long goodDiff) {
            if( select != null ) {
                return select;
            }

            CoreWhLoc shallow = doubleLoc.shallow;
            CoreWhLoc deep = doubleLoc.deep;
            if( deep == null ) {
                return select;
            }
            if( deep.countStock == 0 ) {
                return select;
            }
            if( deep.workingCount() > 0 ) {
                return select;
            }
            if( deep.goodsDiff != goodDiff ) {
                return select;
            }
            if( shallow.countStock == 0 ) {
                return shallow;
            }
            return select;
        }
        //第三种情况,单伸     
        static CoreWhLoc selectSingle(CoreWhLoc select ,DoubleLoc doubleLoc) {

            if( select != null ) {
                return select;
            }
            CoreWhLoc shallow = doubleLoc.shallow;
            CoreWhLoc deep = doubleLoc.deep;
            if( deep != null ) {
                return select;
            }
            if( shallow.countStock == 0 ) {
                return shallow;
            }
            return select;
        }
        //双伸 不同
        static CoreWhLoc selectDoubleDef(CoreWhLoc select ,DoubleLoc doubleLoc ,long goodDiff) {
            CoreWhLoc shallow = doubleLoc.shallow;
            if( select != null ) {
                return select;
            }
            CoreWhLoc deep = doubleLoc.deep;
            if( deep == null ) {
                return select;
            }
            if( deep.countStock == 0 ) {
                return select;
            }
            if( deep.workingCount() > 0 ) {
                return select;
            }
            if( deep.goodsDiff == goodDiff ) {
                return select;
            }
            if( shallow.countStock == 0 ) {
                return shallow;
            }
            return select;
        }


        static List<DoubleLoc> initLoc(List<CoreWhLoc> locList) {
            List<DoubleLoc> list = new List<DoubleLoc>();
            foreach( CoreWhLoc loc in locList ) {
                add(loc ,list);
            }
            check(list);
            return list;
        }
        static void add(CoreWhLoc loc ,List<DoubleLoc> list) {
            bool has = false;
            foreach( DoubleLoc doublelLoc in list ) {
                has = doublelLoc.setLoc(loc);
            }
            if( !has ) {
                DoubleLoc doubleLoc = new DoubleLoc();
                doubleLoc.setLoc(loc);
                list.Add(doubleLoc);
            }
        }
        static void check(List<DoubleLoc> list) {
            List<int> removeScr = new List<int>();
            foreach( DoubleLoc doublelLoc in list ) {
                if( doublelLoc.shallow == null ) {
                    removeScr.Add(doublelLoc.scr);
                    LoggerCommon.fileAll(doublelLoc.scr + "浅货位异常");
                } else if( doublelLoc.deep == null && doublelLoc.shallow.type != 1 ) {
                    removeScr.Add(doublelLoc.scr);
                    LoggerCommon.fileAll(doublelLoc.shallow.id + "不是单伸且深货位不存在");
                }
            }
            foreach( int value in removeScr ) {
                list.RemoveAll(o => { return o.scr == value; });
            }
        }

        public override CoreWhLoc moveSelect(CoreWhLoc src ,IDbConnection Connection ,IDbTransaction transaction) {
            throw new NotImplementedException();
        }
    }


    class DoubleLoc {
        public int scr;
        public CoreWhLoc deep;
        public CoreWhLoc shallow;
        public bool setLoc(CoreWhLoc loc) {
            if( scr == 0 ) {
                scr = loc.scr;
            } else if( scr != loc.scr ) {
                return false;
            }
            if( loc.deepIndex == 1 ) {
                if( shallow != null ) {
                    throw new System.Exception(loc.id + "浅货位异常");
                } else {
                    shallow = loc;
                    return true;
                }
            }
            if( loc.deepIndex == 2 ) {
                if( deep != null ) {
                    throw new System.Exception(loc.id + "深货位异常");
                } else {
                    deep = loc;
                    return true;
                }
            }
            return false;
        }
    }

}
